<template>
  <div>
    <van-nav-bar
      :title="obj.community"
      left-arrow
      @click-left="$router.back()"
      fixed
    />
    <div class="detail">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="(item, index) in obj.houseImg" :key="index"
          ><div
            class="image"
            :style="{
              backgroundImage: 'url(http://liufusong.top:8080' + item + ')',
            }"
          ></div
        ></van-swipe-item>
      </van-swipe>
      <div class="info">
        <div class="title">
          <p>{{ obj.title }}</p>
        </div>
        <div class="tag">
          <span v-for="(i, index) in obj.tags" :key="index">{{ i }}</span>
        </div>
        <van-grid
          :column-num="3"
          :border="false"
          class="van-hairline--top-bottom"
        >
          <van-grid-item>
            <div class="gridDtem">
              <div>{{ obj.price }}<span>/月</span></div>
              <span>租金</span>
            </div>
          </van-grid-item>
          <van-grid-item>
            <div class="gridDtem">
              <div>{{ obj.roomType }}</div>
              <span>房型</span>
            </div>
          </van-grid-item>
          <van-grid-item>
            <div class="gridDtem">
              <div>{{ obj.size }}平米</div>
              <span>面积</span>
            </div>
          </van-grid-item>
        </van-grid>
        <van-row>
          <van-col span="12"><span>装修：</span>精装</van-col>
          <van-col span="12"><span>朝向：</span>{{ obj.oriented[0] }}</van-col>
          <van-col span="12"><span>楼层：</span>{{ obj.floor }}</van-col>
          <van-col span="12"><span>类型：</span>普通住宅</van-col>
        </van-row>
      </div>
      <div class="card">
        <div class="mapTitle">小区：{{ obj.community }}</div>
        <baidu-map
          class="bm-view"
          ak="Ez4CadNKRfZGVC1C1IcMymYsWspdpPLW"
          :center="center"
          :zoom="zoom"
          @ready="handler"
        >
          <bm-label :content="obj.community" :position="center" />
        </baidu-map>
        <div class="mapTitle van-hairline--bottom">
          <h4 style="margin: 0">房屋配套</h4>
        </div>
        <van-grid :column-num="5" :border="false">
          <van-grid-item v-if="obj.supporting.includes('衣柜')">
            <div class="item">
              <i class="iconfont icon-wardrobe HousePackage_icon__2hT31"></i>
              <p>衣柜</p>
            </div>
          </van-grid-item>
          <van-grid-item v-if="obj.supporting.includes('洗衣机')">
            <div class="item">
              <i class="iconfont icon-wash HousePackage_icon__2hT31"></i>
              <p>洗衣机</p>
            </div>
          </van-grid-item>
          <van-grid-item v-if="obj.supporting.includes('空调')">
            <div class="item">
              <i class="iconfont icon-air HousePackage_icon__2hT31"></i>
              <p>空调</p>
            </div>
          </van-grid-item>
          <van-grid-item v-if="obj.supporting.includes('天然气')">
            <div class="item">
              <i class="iconfont icon-gas HousePackage_icon__2hT31"></i>
              <p>天然气</p>
            </div>
          </van-grid-item>
          <van-grid-item v-if="obj.supporting.includes('冰箱')">
            <div class="item">
              <i class="iconfont icon-ref HousePackage_icon__2hT31"></i>
              <p>冰箱</p>
            </div>
          </van-grid-item>
          <van-grid-item v-if="obj.supporting.includes('暖气')">
            <div class="item">
              <i class="iconfont icon-Heat HousePackage_icon__2hT31"></i>
              <p>暖气</p>
            </div>
          </van-grid-item>
          <van-grid-item v-if="obj.supporting.includes('电视')">
            <div class="item">
              <i class="iconfont icon-vid HousePackage_icon__2hT31"></i>
              <p>电视</p>
            </div>
          </van-grid-item>
          <van-grid-item v-if="obj.supporting.includes('热水器')">
            <div class="item">
              <i class="iconfont icon-heater HousePackage_icon__2hT31"></i>
              <p>热水器</p>
            </div>
          </van-grid-item>
          <van-grid-item v-if="obj.supporting.includes('宽带')">
            <div class="item">
              <i class="iconfont icon-broadband HousePackage_icon__2hT31"></i>
              <p>宽带</p>
            </div>
          </van-grid-item>
          <van-grid-item v-if="obj.supporting.includes('沙发')">
            <div class="item">
              <i class="iconfont icon-sofa HousePackage_icon__2hT31"></i>
              <p>沙发</p>
            </div>
          </van-grid-item>
        </van-grid>
      </div>
      <div class="card">
        <div class="mapTitle van-hairline--bottom">
          <h4>房屋概况</h4>
        </div>
        <div class="users">
          <div class="info">
            <img src="http://liufusong.top:8080/img/avatar.png">
            <div class="uname">
              <p>王女士</p>
              <p class="blue"><i class="iconfont icon-auth"></i>认证房主</p>
            </div>
          </div>
          <div class="botton">
            <div>发消息</div>
          </div>
        </div>
        <div>
          1.周边配套齐全，地铁四号线陶然亭站，交通便利，公交云集，距离北京南站、西站都很近距离。 2.小区规模大，配套全年，幼儿园，体育场，游泳馆，养老院，小学。 3.人车分流，环境优美。 4.精装两居室，居家生活方便，还有一个小书房，看房随时联系。
        </div>
      </div>
      <div class="card">
        <div class="mapTitle van-hairline--bottom">
          <h4>猜你喜欢</h4>
        </div>
        <HouseList v-for="(item, index) in list" :key="index" :item="item" :off="true"/>
      </div>
      <van-row class="bottomBar">
        <van-col span="8"
          ><van-button type="default" block @click="starFn"
            ><van-icon :name="star" />&nbsp;收藏</van-button
          ></van-col
        >
        <van-col span="8"
          ><van-button type="default" block>在线咨询</van-button></van-col
        >
        <van-col span="8"
          ><van-button type="info" block>电话咨询</van-button></van-col
        >
      </van-row>
    </div>
  </div>
</template>

<script>
import { Dialog } from 'vant'
import '@/icon/iconfont.css'
import BaiduMap from 'vue-baidu-map/components/map/Map.vue'
import BmLabel from 'vue-baidu-map/components/overlays/Label.vue'
import { getDetial, getStar, opstStar, delStar } from '@/api/homo'
import HouseList from '@/components/HouseList'
import { getToken } from '@/utils/storage'
export default {
  components: {
    BaiduMap,
    BmLabel,
    HouseList
  },
  data () {
    return {
      houseID: '',
      obj: {},
      star: 'star-o',
      center: { lng: 0, lat: 0 },
      zoom: 0,
      list: [
        {
          desc: '72.32㎡/南 北/低楼层',
          houseCode: 'b77a6391-c018-1785',
          houseImg: '/img/message/1.png',
          price: 4500,
          tags: ['随时看房'],
          title: '安贞西里 3室1厅'
        },
        {
          desc: '83㎡/南/高楼层',
          houseCode: 'b77a6391-c018-1785',
          houseImg: '/img/message/2.png',
          price: 7200,
          tags: ['近地铁'],
          title: '天居园 2室1厅'
        },
        {
          desc: '52㎡/西南/低楼层',
          houseCode: 'b77a6391-c018-1785',
          houseImg: '/img/message/3.png',
          price: 7300,
          tags: ['集中供暖'],
          title: '角门甲4号院 1室1厅'
        }
      ]
    }
  },
  created () {
    this.houseID = this.$route.params.houses
    getDetial(this.houseID).then((res) => {
      this.obj = res.body
    })
    // 获取收藏
    if (getToken()) {
      getStar(this.houseID).then((res) => {
        if (res.body.isFavorite) {
          this.star = 'star'
        }
      })
    }
  },
  methods: {
    async starFn () {
      if (getToken()) {
        if (this.star === 'star') {
          await delStar(this.houseID)
          this.star = 'star-o'
        } else {
          await opstStar(this.houseID)
          this.star = 'star'
        }
      } else {
        Dialog.confirm({
          confirmButtonText: '登录',
          title: '提示',
          message: '您还没有登录'
        }).then(() => {
          this.$router.push('/login')
        }).catch(() => {
          // 放着不给报错哼唧
        })
      }
    },
    handler ({ BMap, map }) {
      console.log(BMap, map)
      this.center.lng = this.obj.coord.longitude
      this.center.lat = this.obj.coord.latitude
      this.zoom = 15
    }
  }
}
</script>

<style lang="less" scoped>
.van-grid {
  margin-top: 2px;
  .item {
    text-align: center;
    i {
      font-size: 25px;
    }
  }
}
.bm-view {
  width: 100%;
  height: 150px;
  ::v-deep .BMapLabel {
    font-size: 13px !important;
    background: #ee5d5b !important;
    border: 1px solid #ff0000 !important;
    color: #fff;
  }
}
.detail {
  background-color: #f6f5f6;
  margin-top: 46px;
  padding-bottom: 50px;
  .info {
    background-color: #fff;
    padding: 15px;
    .title {
      margin: 15px 0;
    }
    .van-grid-item .gridDtem {
      text-align: center;
      div {
        color: #fa5741;
        font-size: 18px;
        font-weight: 700;
        span {
          color: #fa5741;
          font-size: 12px;
        }
      }
      span {
        font-size: 14px;
        color: #999;
      }
    }
    .van-col {
      padding-top: 15px;
      font-size: 12px;
      span {
        color: #999;
      }
    }
  }
  .tag {
    overflow: hidden;
    height: 21px;
    margin-bottom: 15px;
    span {
      font-size: 12px;
      padding: 4px;
      margin-right: 4px;
      border-radius: 5px;
      background-color: #e1f5f8;
      color: #39becd;
    }
    span:nth-child(2n) {
      background-color: #e1f5ed;
      color: #3fc28c;
    }
    span:nth-child(3n) {
      background-color: #e6f2ff;
      color: #5aabfd;
    }
  }
}
.my-swipe .van-swipe-item {
  height: 250px !important;
  .image {
    width: 100%;
    height: 100%;
    background-size: cover;
  }
}
.card {
  background: white;
  margin-top: 10px;
  .mapTitle {
    padding: 10px;
    font-size: 14px;
    margin-bottom: 1px;
    h4 {
      margin: 0;
    }
  }
  .users {
    display: flex;
    .botton {
      width: 75px;
      display: flex;
      align-items: center;
      div{
        width: 100%;
        border: 1px solid @blue;
        height: 30px;
        line-height: 30px;
        text-align: center;
        border-radius: 5px;
        color: @blue
      }
    }
    .info {
      flex: 1;
      display: flex;
      font-size: 12px;
      line-height: 1.5em;
      img {
        width: 52px;
        height: 52px;
        margin-right: 10px;
      }
      .blue {
        color: #fa5741;
      }
    }
  }
}
.bottomBar {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  button {
    height: 50px;
    font-size: 18px;
  }
  .van-icon-star {
    color: #ff4f00;
  }
}
</style>
